package com.taobao.machinesystem.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.taobao.machinesystem.common.Result;
import com.taobao.machinesystem.common.SystemConst;
import com.taobao.machinesystem.entity.McInspect;
import com.taobao.machinesystem.entity.SysUser;
import com.taobao.machinesystem.entity.vo.McInspectQueryVo;
import com.taobao.machinesystem.service.McInspectService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * <p>
 * 单次巡查表 前端控制器
 * </p>
 *
 * @author LiuJunCheng
 * @since 2022-04-22
 */
@Slf4j
@RestController
@RequestMapping("/machine_system/mc-inspect")
public class McInspectController {

    @Autowired
    private McInspectService mcInspectService;

    /**
     * 创建年度机房单词巡查计划
     * @param mcInspect
     * @return Result
     */
    @PostMapping("/create")
    public Result createMcInspect(@RequestBody McInspect mcInspect) {
        System.out.println("**************************");
        System.out.println(mcInspect);
        System.out.println("**************************");
        if (mcInspect==null) {
            return Result.error().setMessage(SystemConst.NOT_NULL_PARAMETER);
        }
        Subject subject = SecurityUtils.getSubject();
        SysUser principal = (SysUser)subject.getPrincipal();
        mcInspect.setCreatedBy(principal.getUsername());
        boolean flag = mcInspectService.createMcInspect(mcInspect);
        if (flag) {
            log.info("create plan success {}", mcInspect.toString());
            return Result.ok().setMessage(SystemConst.CREATE_SUCCESS);
        } else {
            log.error("create plan fail {}", mcInspect.toString());
            return Result.error().setMessage(SystemConst.CREATE_FAIL);
        }
    }

    /**
     * 分页查询
     * @param currentPage
     * @param size
     * @param mcInspectQueryVo
     * @return Result
     */
    @PostMapping("/list/{currentPage}/{size}")
    public Result listMcInspect0(
            @PathVariable("currentPage") Integer currentPage,
            @PathVariable("size")Integer size,
            @RequestBody McInspectQueryVo mcInspectQueryVo){
        System.out.println("------------------");
        System.out.println(mcInspectQueryVo);
        System.out.println("------------------");
        Page<McInspect> mcInspectPage =  mcInspectService.listMcInspect(currentPage,size,mcInspectQueryVo);

        return Result.ok()
                .setMessage(SystemConst.SELECT_SUCCESS)
                .setMapValue("total",mcInspectPage.getTotal())
                .setMapValue("items",mcInspectPage.getRecords());
    }

    /**
     * 跟新操作
     * @param mcInspect
     * @return Result
     */
    @PutMapping("/update")
    public Result updateMcInspect(@RequestBody McInspect mcInspect) {
        if (!Objects.isNull(mcInspect)) {
            return Result.error().setMessage(SystemConst.NOT_NULL_PARAMETER);
        }

        boolean flag = mcInspectService.updateById(mcInspect);
        if (flag) {
            log.info("update plan0 success {}", mcInspect.toString());
            return Result.ok().setMessage(SystemConst.UPDATE_SUCCESS);
        } else {
            log.error("update plan0 fail {}", mcInspect.toString());
            return Result.error().setMessage(SystemConst.UPDATE_FAIL);
        }
    }

    /**
     * 删除计划
     * @param id
     * @return Result
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteMcInspect0(@PathVariable String id){
        boolean flag = mcInspectService.removeById(id);
        if (flag) {
            log.info("update plan success {}", id);
            return Result.ok().setMessage(SystemConst.DELETE_SUCCESS);
        } else {
            log.error("update plan fail {}", id);
            return Result.error().setMessage(SystemConst.DELETE_FAIL);
        }
    }
}

